package com.deng.controller;

import com.deng.Service.TagService;
import com.deng.model.Paper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
import java.util.Set;

/**
 * @Author 邓小熙
 * @Date 2022/5/17 17:49
 * @Version 1.0
 */
@Controller
public class TagController {
    @Autowired
    TagService tagService;
    /**
     * 主页查看所有tag
     * @return
     */
    @RequestMapping(value = {"/main/tag/tags","/tag/tags"})
    public @ResponseBody
    Map<String, Set<String>> AllTags(){
        Map<String,Set<String>> map = Maps.newHashMap();
        map.put("tags",tagService.findAllTagName());
        return map;
    }
    /**
     * 返回前台的tagList页面
     * @param tagName
     * @return
     */
    @RequestMapping(value = "/main/tag/returnTagPage/{tagName}")
    public @ResponseBody
    ModelAndView returnTagPage(@PathVariable("tagName") String tagName){
        ModelAndView modelAndView = new ModelAndView("/main/tagList");
        modelAndView.addObject("tagName",tagName);
        return modelAndView;
    }

    /**
     * 根据tagName查找文章 并进行分页
     * @param tagName
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/main/tag/tagPages/{pageNum}")
    public @ResponseBody
    PageInfo<Paper> tagPages(@RequestParam("tagName") String tagName, @PathVariable("pageNum") int pageNum){
        return tagService.tagPage(tagName,pageNum);
    }

    /**
     * 返回后台的tag管理页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/tag/tagPage/{id}")
    public String tagPage(@PathVariable("id") int id){
        return "InnerLayui/caozuo/tagMes";
    }
    /**
     * 查询对应文章的所有tag
     * @param id
     * @return
     */
    @RequestMapping(value = "/tag/tagsByPaperId/{id}")
    public @ResponseBody Map<String, Object> tagsByPaperId(@PathVariable("id") int id){
        return tagService.findTagNamesByPaperId(id);
    }
    /**
     * 新建标签
     * @param name
     * @param id
     * @return
     */
    @RequestMapping(value = "/tag/insertTags/{id}")
    public @ResponseBody int insertTags(@RequestParam("newTag") String name,@PathVariable("id") int id){
        return tagService.insertTagNameByPaperId(name, id);
    }
    /**
     * 删除tag
     * @param id
     * @param tags
     * @return
     */
    @RequestMapping(value = "/tag/delectTags/{id}/{tags}",method = RequestMethod.POST)
    public @ResponseBody int delectTags(@PathVariable("id") int id,@PathVariable("tags") String tags){
        return tagService.delectTagByPaperIdAndTagName(id,tags);
    }
    /**
     * 返回修改tag的页面
     * @return
     */
    @RequestMapping(value = "/tag/updateTag")
    public String updateTag(){
        return "InnerLayui/caozuo/updateTag";
    }
    /**
     * 后台修改tag
     * @param id
     * @param oldTag
     * @param newTag
     * @return
     */
    @RequestMapping(value = "/tag/updateTags/{id}/{oldTag}")
    public @ResponseBody int updateTags(@PathVariable("id") int id,@PathVariable("oldTag") String oldTag,@RequestParam("title") String newTag){
        return tagService.updateTagNameByPaperIdAndTagName(newTag,id,oldTag);
    }





    /**
     * 查找对应用户的所有tag
     * @param name
     * @return
     */
    @RequestMapping(value = "/tag/findAllTagsByName/{name}")
    public @ResponseBody Map<String,Object> findAllTagsByName(@PathVariable("name") String name){
        Set<String> list = tagService.findTagNameByUser(name);
        Map<String ,Object> map = Maps.newHashMap();
        boolean flag = true ;
        if(list.size() == 0){
            flag = false;
        }
        map.put("flag",flag);
        map.put("list",list);
        return map;
    }
}
